package com.zhz.leetcode;

import com.zhz.leetcode.entity.ListNode;
import com.zhz.leetcode.utils.LinkedListUtils;

/**
 * 203、移除链表元素
 * https://leetcode-cn.com/problems/remove-linked-list-elements/
 * 给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点 。
 */
public class RemoveLinkedListElement203 {

    public static void main(String[] args) {
        RemoveLinkedListElement203 m = new RemoveLinkedListElement203();
        ListNode node7 = new ListNode(6);
        ListNode node6 = new ListNode(5, node7);
        ListNode node5 = new ListNode(4, node6);
        ListNode node4 = new ListNode(3, node5);
        ListNode node3 = new ListNode(6, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);

//        输入：head = [1,2,6,3,4,5,6], val = 6
//        输出：[1,2,3,4,5]
        ListNode head = m.removeElements(node1, 6);
        LinkedListUtils.printList(head);
    }

    public ListNode removeElements(ListNode head, int val) {
        ListNode first = new ListNode(0);
        first.next = head;
        ListNode tmpNode = first;
        while (tmpNode.next != null) {
            if (tmpNode.next.val == val) {
                tmpNode.next = tmpNode.next.next;
            }else{
                tmpNode = tmpNode.next;
            }
        }
        return first.next;

    }

}
